package com.jboost.modules.app.util;

import com.jboost.modules.app.constant.BaseEnumInterface;
import com.jboost.modules.app.constant.LiftGate_enum;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Objects;
import java.util.Optional;

//只适用于实现了BaseEnumInterface接口的枚举
public class EnumUtils {
    static BaseEnumInterface[] valueInterFaceArray(Class clazz) throws Exception {
        //获取所有枚举实例
        Enum[] enumConstants = (Enum[]) clazz.getEnumConstants();
        return (BaseEnumInterface[]) enumConstants;
    }

    ;

    public static String getDescByType(Class clazz, Object type) {
        Optional<BaseEnumInterface> first = null;
        try {
            first = Arrays.stream(valueInterFaceArray(clazz)).filter(e -> {
                return Objects.equals(type, e.getType());
            }).findFirst();
        } catch (Exception e) {
            return null;
        }
        if (!first.isPresent()) {
            return null;
        }
        BaseEnumInterface baseInterface = (first.get());
        if (Objects.nonNull(baseInterface)) {
            return baseInterface.getDesc();
        }
        return null;
    }


    public static void main(String[] args) {

    }


    public static Enum getEnumByType(Class clazz, Object type) {
        Optional<BaseEnumInterface> first = null;
        try {
            first = Arrays.stream(valueInterFaceArray(clazz)).filter(e -> {
                return Objects.equals(type, e.getType());
            }).findFirst();
        } catch (Exception e) {
            return null;
        }
        if (!first.isPresent()) {
            return null;
        }
        BaseEnumInterface baseInterface = (first.get());
        if (Objects.nonNull(baseInterface)) {
            return (Enum) baseInterface;
        }
        return null;
    }

}
